<?php

namespace Common\Model;

use Think\Model;

class AbilityCheckQuestionModel extends Model {

    /**
     * 获取题目列表
     * @param int $iCateId 岗位ID
     * @param int $iRank 级别：1-初级 2-中级 3-高级
     */
    public function getQuestionList($iCateId, $iRank) {
        //中级所有题目列表
        $arrWhere['acq.status'] = 1;
        $arrWhere['al.rank'] = $iRank;   //中级
        $arrWhere['al.cate_id'] = $iCateId;
        $arrQuestionList = $this->table('qy_ability_check_question acq')
                ->field('acq.ability_check_question_id,acq.type,acq.third_ability_level_id,acq.title,acq.question_img,acq.addition')
                ->join('qy_ability_level as al ON al.ability_level_id = acq.first_ability_level_id')
                ->where($arrWhere)
//                ->cache(TRUE)
                ->select();
        if (empty($arrQuestionList)) {
            return FALSE;
        }
        foreach ($arrQuestionList as &$val) {
            $val['question_img'] = json_decode($val['question_img']);
            if(!empty($val['question_img'])){
                foreach($val['question_img'] as &$vv){
                    $vv = C('ADMIN_URL').$vv;
                }
                unset($vv);
            }
            $arrQuestionGroupList[$val['third_ability_level_id']][] = $val;
        }
        //获取中级三层设置的题数
        $arrThirdLevelList = $this->query("SELECT ability_level_id, quesion_total FROM qy_ability_level WHERE quesion_total != 0 AND parent_id IN (SELECT ability_level_id FROM qy_ability_level WHERE parent_id IN (SELECT ability_level_id FROM qy_ability_level WHERE cate_id = {$iCateId}))");
        if (empty($arrThirdLevelList)) {
            return FALSE;
        }
        $singleQuestionList = array();  //单选题目
        $multipleQuestionList = array();  //多选题目
        foreach ($arrThirdLevelList as $val) {
            if (!empty($arrQuestionGroupList[$val['ability_level_id']])) {
                //单个三级数组中随机取题
                $oneGroupList = array();
                $oneGroupList = $arrQuestionGroupList[$val['ability_level_id']];
                //注意：array_rand 如果count(参数1)小于参数2，返回NULL； 如果参数2为1返回一个整数；
                $oneGroupListKey = array_rand($oneGroupList, $val['quesion_total']);
                if(count($oneGroupListKey) == 1){   //非数组
                    $oneGroupListKey = array($oneGroupListKey);
                }
                foreach ($oneGroupListKey as $vv) {
                    if ($oneGroupList[$vv]['type'] == 1) {
                        $multipleQuestionList[] = $oneGroupList[$vv];
                    } else {
                        $singleQuestionList[] = $oneGroupList[$vv];
                    }
                }
            }
        }
        //合并单选和多选，注意：由于需求规定只取配置的题数，并没有说是单选多少或是多选多少，所以有可能全部是单选题，或者全部是多选题。
        $returnList = array_merge($singleQuestionList, $multipleQuestionList);
        foreach ($returnList as &$val) {
            $val['addition'] = json_decode($val['addition'], TRUE);
        }
        unset($val);
        return $returnList;
    }

    

}
